package org.skipper.oss.config;

import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.skipper.oss.OssClient;
import org.skipper.oss.domain.AliyunOssProperties;
import org.skipper.oss.domain.OssProperties;
import org.skipper.oss.impl.OssClientImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2024-03-07
 * Time: 15:16
 */
@Configuration
@EnableConfigurationProperties({OssProperties.class})
@Slf4j
public class OssAutoConfiguration implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    private List<String> ossServerNames = new ArrayList<>();

    private DefaultListableBeanFactory defaultListableBeanFactory;

    @Bean
    public BeanPostProcessor ossBeanPostProcessor(OssProperties properties) throws IOException {
        registerOssClient(properties);
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object arg0,
                                                         String arg1) throws BeansException {
                return arg0;
            }

            @Override
            public Object postProcessBeforeInitialization(Object arg0,
                                                          String arg1) throws BeansException {
                return arg0;
            }
        };
    }

    private void registerOssClient(OssProperties properties) {
        log.info("----registerOssClient----");
        if (properties != null) {
            AliyunOssProperties[] aliyunOssProperties = properties.getAliyun();

            if (!CollectionUtils.isEmpty(ossServerNames) && aliyunOssProperties != null) {
                for (AliyunOssProperties aliyunOssProperty : aliyunOssProperties) {
                    if (ossServerNames.contains(aliyunOssProperty.getName())) {
                        OssClient ossClient = OssClientImpl.create(aliyunOssProperty);
                        defaultListableBeanFactory.registerSingleton(aliyunOssProperty.getName(), ossClient);
                    }
                }
            }
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        setOssSourceNames(applicationContext);
        // 获取BeanFactory
        this.defaultListableBeanFactory = (DefaultListableBeanFactory) this.applicationContext
                .getAutowireCapableBeanFactory();
    }

    /**
     * 设置oss源名称，支持多个oss源配置
     *
     * @param applicationContext 应用程序上下文
     */
    private void setOssSourceNames(ApplicationContext applicationContext) {
        String singleSourceNames = applicationContext.getEnvironment().getProperty("oss.names");

        if (!StringUtils.isEmpty(singleSourceNames)) {
            ossServerNames.addAll(Splitter.on(",").trimResults().splitToList(singleSourceNames));
        }
    }

}
